//
//  DDLoginManager.m
//  Dingdong
//
//  Created by 程强 on 16/11/17.
//  Copyright © 2016年 程强. All rights reserved.
//

#import "DDLoginManager.h"
#import "KKDES.h"
#import "DDUserObj.h"

@implementation DDLoginManager
- (DSASubject *)loginIn:(NSString *)userName password:(NSString *)pwd
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/login_in.do", ServerUrl];

    NSDictionary *dic = @{@"username":userName,@"password":pwd};
    NSData *body = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
    NSString *jsonStr = [[ NSString alloc] initWithData:body encoding:NSUTF8StringEncoding];
    NSString *encrytJson = [KKDES encryptUseDES:jsonStr key:DESKey];
    NSDictionary *pramaDic = @{@"loginToken":encrytJson};
    
    [KKHttpServices httpPostUrl:doVerifyurl prams:pramaDic success:^(id responseObject, KKHttpParse *parse) {
        NSDictionary *retDic = parse.responseJsonOB;
        [subject sendNext:retDic];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;
}
- (DSASubject *)registerUser:(NSString *)email password:(NSString *)pwd gender:(NSString *)gender name:(NSString *)name
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/member/register.do", ServerUrl];
    
    NSString *encryPwd = [KKDES encryptUseDES:pwd key:DESKey];
    NSDictionary *dic = @{@"password":encryPwd,
                          @"email":email,
                          @"gender":gender,
                          @"name":name};
    
    [KKHttpServices httpPostUrl:doVerifyurl prams:dic success:^(id responseObject, KKHttpParse *parse) {
        NSArray *ary = (NSArray *)parse.responseJsonOB;
        [subject sendNext:ary];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;
}
- (DSASubject *)logout:(NSString *)token
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/login_out.do", ServerUrl];
    NSDictionary *body = @{@"token":token};
    NSString *bodyStr = [ToolKit JSONEncodeObj:body];
    NSString *sign = @"";
    sign = [sign stringByAppendingString:bodyStr];
    sign = [sign stringByAppendingString:token];
    sign = [sign stringByAppendingString:AppVersion];
    sign = [sign stringByAppendingString:NowTimeStamp];
    NSString *md5Sign = [sign md5Encrypt];
    
    [KKHttpServices httpPostUrl:doVerifyurl sign:md5Sign prams:nil success:^(id responseObject, KKHttpParse *parse) {
        NSArray *ary = (NSArray *)parse.responseJsonOB;
        [subject sendNext:ary];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;
}
- (DSASubject *)retrievePasswordVerifyCode:(NSString *)email
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/member/sendEmailCode.do", ServerUrl];
    
    NSDictionary *body = @{@"email":email};
    NSString *sign = [SystemConfig httpSignWithBody:body];
    
    [KKHttpServices httpPostUrl:doVerifyurl sign:sign prams:body success:^(id responseObject, KKHttpParse *parse) {
        NSArray *ary = (NSArray *)parse.responseJsonOB;
        [subject sendNext:ary];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;
}
- (DSASubject *)retrievePassword:(NSString *)email code:(NSString *)code pwd:(NSString *)password confirmPwd:(NSString *)cpwd
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/member/passwordReset.do", ServerUrl];
    
    NSDictionary *prama = @{@"email":email,@"code":code,@"password0":password,@"password1":cpwd};
    NSString *jsonPrama = [ToolKit JSONEncodeFromDictionary:prama.mutableCopy];
    NSString *desJson = [KKDES encryptUseDES:jsonPrama key:DESKey];
    
    NSDictionary *body = @{@"data":desJson};
    NSString *sign = [SystemConfig httpSignWithBody:body];
    
    [KKHttpServices httpPostUrl:doVerifyurl sign:sign prams:body success:^(id responseObject, KKHttpParse *parse) {
        NSArray *ary = (NSArray *)parse.responseJsonOB;
        [subject sendNext:ary];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;

}
- (DSASubject *)getUserInfo
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/member/get.do", ServerUrl];
    
    NSDictionary *body = nil;
    NSString *sign = [SystemConfig httpSignWithBody:body];
    
    [KKHttpServices httpPostUrl:doVerifyurl sign:sign prams:body success:^(id responseObject, KKHttpParse *parse) {
        NSDictionary *rdic = parse.responseJsonOB;
        DDUserObj *obj = [DDUserObj modelWithDictionary:rdic[@"data"]];
        [SystemConfig shareInstance].user = obj;
        [[NSNotificationCenter defaultCenter]postNotificationName:@"LoadUserInfo" object:nil];
        [subject sendNext:obj];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;
}

@end
